<%
if (session.getAttribute("username") == null) {
	%><jsp:forward page="login.jsp" /><%
}
%>
<jsp:useBean id="user" class="bean.PollUser" scope="request" />
<jsp:setProperty name="user" property="*"/>
<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK" import="bean.PollUser, model.PollUserImpl, connection.DbConnection, java.sql.*, java.util.ArrayList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
<link rel="stylesheet" href="style/stylesheet.css" type="text/css" >
<title>dbPoll/Poll Administration: Open / Close Voting</title>
<script type="text/javascript">

    function check(form){

        // Clear success msgs
        document.getElementById("success_msg").innerHTML = "";
           
        if(document.forms.form1.define_poll.value==""){
            document.getElementById("define_poll_msg").innerHTML = "<div class=\"error\" style=\"display: block; \" >* Please add poll definition. </div>";
            document.forms.form1.define_poll.focus();   
            return false;   
        } else {
        	document.getElementById("define_poll_msg").innerHTML = "";
        }
        return true;
    }

    function set_poll (form) {
    }

</script>
</head>

<body>
<div class="bg">
<div class='content' >
<jsp:include page="nav.jsp" />
	<div id='title' class= "pagetitle">Administrative User Assignment<input type="button" style="font-size:8pt;color:white;background-color:lightblue;border:none;" value="Quick Help"></div>
	
	
	<%
	//poll name
	String pollname = (String)session.getAttribute("pname");
	
	// User Array
	ArrayList<String> PollCreators = new ArrayList<String>();
	ArrayList<String> PollMasters = new ArrayList<String>();
	ArrayList<String> PollCreators_inPoll = new ArrayList<String>();
	ArrayList<String> PollMasters_inPoll = new ArrayList<String>();
	
	// Genrate PollCreators and Masters in this poll list
	String sql = "Select PollUser.Username, PollUser.Accessibility from User_Poll,PollUser where User_Poll.Username=PollUser.Username and User_poll.pollid="+session.getAttribute("selected_poll");
	DbConnection db = new DbConnection();
	System.out.println("assignUser table PC SQL: "+sql);
	ResultSet rs = db.doSelect(sql);
	
	while (rs.next()) {
		if (rs.getInt(2) == 3) {
			PollCreators_inPoll.add(rs.getString(1));
		}
		if (rs.getInt(2) == 2) {
			PollMasters_inPoll.add(rs.getString(1));
		} 
	}
	
	// Generate PollCreators and Masters in Total Poll List
	sql="Select Username, Accessibility from PollUser";
	rs = db.doSelect(sql);
	while (rs.next()) {
		if (rs.getInt(2) == 3) {
			PollCreators.add(rs.getString(1));
		}
		if (rs.getInt(2) == 2) {
			PollMasters.add(rs.getString(1));
		}
	}
	
		// Only show poll creator assignment if System Admin or Poll Admin
		if (session.getAttribute("accessibility") != null && (session.getAttribute("accessibility").equals(5) || session.getAttribute("accessibility").equals(4))) {
	%>
	<div class="entrytitle">
	<div class="entry">
	
		<div class="tableborder"><table class="blockTable">
		<thead><tr><td colspan="3">Poll Creators Currently Assigned to Poll</td></tr></thead>
		<tbody>
		<%
		// Only generate table if poll selected
		if (session.getAttribute("selected_poll") == null) {
			out.println("<tr><td colspan=\"3\">Please select poll and click generate</td></tr>");
		} else {
		
			// Genrate table rows
			for (int i = 0; i < PollCreators_inPoll.size(); i++) {
				out.println("<tr><td colspan=\"3\">"+PollCreators_inPoll.get(i)+"</td></tr>");
			}
		
		%>
		<form action="addPollCreatorServlet" method="post">
		<tr><td>Add Poll Creator</td><td><select name="poll_creator"><%
			// Generate Poll Creator List

			for (int i = 0; i < PollCreators.size(); i++) {
				out.println("<option value=\""+PollCreators.get(i)+"\" >"+PollCreators.get(i)+"</option>");
			}
		%></select></td><td><input type="submit" value="Add" class="button" /></td></tr>
		</form>
		<%
		}
		%>
		</tbody></table>
		
	</div></div></div>
	<%
		}
	%>
	
	
	<%	
		// Only show poll creator assignment if System Admin or Poll Admin or Poll Creator
		if (session.getAttribute("accessibility") != null && (session.getAttribute("accessibility").equals(5) || session.getAttribute("accessibility").equals(4) || session.getAttribute("accessibility").equals(3))) {
	%>
	<div class="entrytitle">
	<div class="entry">
	
		<div class="tableborder"><table class="blockTable">
		<thead><tr><td colspan="3">Poll Masters currently assigned to "<%=pollname %>"</td></tr></thead>
		<tbody>
		<%
		// Only generate table if poll selected
		if (session.getAttribute("selected_poll") == null) {
			out.println("<tr><td colspan=\"3\">Please select poll and click generate</td></tr>");
		} else {
		
			// Genrate table rows
			/*
			sql = "Select PollUser.Username, PollUser.Accessibility from User_Poll,PollUser where User_Poll.Username=PollUser.Username and User_poll.pollid="+session.getAttribute("selected_poll");
			System.out.println("assignUser table PM SQL: "+sql);
			ResultSet rs2 = db.doSelect(sql);
			while (rs2.next()) {
				out.println("<tr><td colspan=\"3\">"+rs2.getString("Username")+"</td></tr>"); 
			}
			*/
			for (int i = 0; i < PollMasters_inPoll.size(); i++) {
				out.println("<tr><td colspan=\"3\">"+PollMasters_inPoll.get(i)+"</td></tr>");
			}
			
		
		%>
		<form action="addMasterCreatorServlet" method="post">
		<tr><td>Add Poll Master</td><td><select name="poll_master"><%
			// Generate Poll Creator List
			/*
			sql="Select Username from PollUser where accessibility = 2";
			rs2 = db.doSelect(sql);
			while (rs2.next()) {
				out.println("<option value=\""+rs2.getString(1)+"\" >"+rs2.getString(1)+"</option>");
			}
			*/
			for (int i = 0; i < PollMasters.size(); i++) {
				out.println("<option value=\""+PollMasters.get(i)+"\" >"+PollMasters.get(i)+"</option>");
			}
		%></select></td><td><input type="submit" value="Add" class="button" /></td></tr>
		</form>
		<%
		}
		%>
		</tbody></table>
		
	</div></div>
	<%
		}
		
	%>
	
	
	</div></div>
<style>
#onlinehelp{position:absolute;display:none;z-index:2;border:5px solid lightblue;background-color:#33aecc;color:white}
</style>
<div id="onlinehelp"></div>
<script>
var demo = document.getElementById("title");
var gg = demo.getElementsByTagName("input");
var ei = document.getElementById("onlinehelp");
for(i=0; i<gg.length; i++){
 var ts = gg[i];
 ts.onmousemove = function(event){
  event = event || window.event;
  ei.style.display = "block";
  ei.innerHTML = '1. Choose a poll to assign and click the "Generate" button <br>2. Select a user to assign and click "Add" button<br> * For more help please click the button';
  ei.style.top  = document.body.scrollTop + event.clientY  -65 + "px";
  ei.style.left = document.body.scrollLeft + event.clientX  +35 + "px";
 }
 ts.onmouseout = function(){
  ei.innerHTML = "";
  ei.style.display = "none";
 }
 ts.onclick = function(){
	 location.href="/DBP/OnlineHelp/ModifyAccount.jsp";
 }
}
</script>	
</body>
</html>